 @include "lib.awk"
 function power(w,goals,bests,rests,d,out,\
                good,bad,max,x) {
   counts(bests,w,goals,d,good) # count ranges in good and bad
   counts(rests,w,goals,d,bad)
   max = powerRanges(good,bad,  # score the ranges
                     length(bests),length(rests),out)
   for(x in out)
     if(out[x]["val"] < max/3) 
       delete out[x]            # remove dull ranges
   return length(out)
 }  
 function counts(rows,w,goals,d,count,   row,col,val) {
   for(col in w)
     if( ! (col in goals))
       for(row in rows)
         if ( (val = d[row][col]) !~ "?")
           count[col][val]++  # frequency of value in column 
 }
 function powerRanges(good,bad,best,rest,out,  \
                      val,b,r,col,tmp,n, score, max) {
   for(col in good) 
     for(val in good[col]) {    # look at all the goods
       b = good[col][val] 
       r = 0                    # default value in "bad"
       if (col in bad)          # look for the real value in "bad"
         if(val in bad[col]) 
           r = bad[col][val]   
       b = b / best  # normalize  w.r.t. total size
       r = r / rest  # dito
       if (b > r) {  # if best's normalized score is better than rest
         n++
         tmp[n]["val"] = score = b^2 / (b+r) # score it
         tmp[n]["col"] = col
         tmp[n][  "="] = val
         if (score > max) max = score
       }}
   asort(tmp,out,"valasc") # sort all generated scores
   return max
 }
